home *** CD-ROM | disk | FTP | other *** search
/ HyperLib 1997 Winter - Disc 1 / HYPERLIB-1997-Winter-CD1.ISO.7z / HYPERLIB-1997-Winter-CD1.ISO / オンラインウェア / BUS / LogicSim 3.0b Folder.sit / LogicSim 3.0b Folder / LogicSim 3.0b ƒ / LogicSim SDK 2.7 / Counter1 ƒ / Counter1.p < prev    next >
Text File  |  1995-09-18  |  4KB  |  159 lines

  1. unit Counter1;
  2.  
  3. interface
  4.  
  5.     uses
  6.         LogicSimIntf;
  7.  
  8. {In CodeWarrior Pascal the entry of the code resource is just after $MAIN}
  9. {In Think Pascal you would simply call the entry procedure "main" like that:}
  10. { procedure main(var blk: LSBlock) }
  11.  
  12.  
  13. {this project uses CodeWarrior...}
  14. {$MAIN}
  15.  
  16.     procedure ComponentProc (var blk: LSBlock);{Entry of the code ressource}
  17.  
  18. implementation
  19.  
  20. uses PascalA4;
  21.  
  22.  
  23.     const
  24.         CLASS_NAME = 'Counter1';
  25.         PICT_ID = 128;
  26.  
  27.     type
  28.         ClassDataR = record
  29.                 pic: PicHandle;
  30.             end;
  31.         ClassDataP = ^ClassDataR;
  32.         ClassDataH = ^ClassDataP;
  33.  
  34.  
  35.         GateDataR = record
  36.                 oldClock: SValue;
  37.                 value: SValue;
  38.             end;
  39.         GateDataP = ^GateDataR;
  40.         GateDataH = ^GateDataP;
  41.  
  42.         
  43.     procedure DoNewClass (var blk: LSBlock);
  44.     {build a new class}
  45.         var
  46.             picH: Handle;
  47.             err: OSErr;
  48.             delaysP: DelaysPtr;
  49.     begin
  50.         picH := GetResource('PICT', PICT_ID);
  51.         HNoPurge(picH);
  52.         DetachResource(picH);{important because the file is going to be closed}
  53.         
  54.         blk.prm.prmClass^.image := Pointer(picH); {this picture is used in the list of components}
  55.         blk.prm.prmClass^.name := CLASS_NAME;
  56.         SetClassID(blk.prm.prmClass^.id, CLASS_NAME);
  57.  
  58.         delaysP := NIL; {default delays = (0,0,0,0,0,0) }
  59.         LSDeclarePin(blk.env, blk.classRef, GetPoint(0,5), kInputPin, delaysP, 'T');
  60.         LSDeclarePin(blk.env, blk.classRef, GetPoint(0,15), kInputPin, delaysP, 'CK');
  61.         LSDeclarePin(blk.env, blk.classRef, GetPoint(50,5), kOutputPin, delaysP, 'OUT');
  62.         LSDeclarePin(blk.env, blk.classRef, GetPoint(50,15), kOutputPin, delaysP, 'C');
  63.  
  64.         {the pins are numbered in the order used for their declaration}
  65.         { so here T is pin #1, CK pin #2, OUT pin #3, C pin #4}
  66.         
  67.         SetHandleSize(Handle(blk.classData), sizeof(ClassDataR)); {prepare the class private storage}
  68.         err := HandToHand(picH); {copy the picture}
  69.         ClassDataH(blk.classData)^^.pic := Pointer(picH);
  70.     end;
  71.  
  72.  
  73.     procedure DoDraw (var blk: LSBlock);
  74.     {draw the symbol of the component}
  75.     begin
  76.         DrawPicture(ClassDataH(blk.classData)^^.pic, blk.prm.prmSymbol^.frame);
  77.     end;
  78.  
  79.     procedure DoNewGate (var blk: LSBlock);
  80.     {create a new gate}
  81.     begin
  82.         SetHandleSize(Handle(blk.gateData), sizeof(GateDataR));
  83.     end;
  84.  
  85.     procedure DoResetGate (var blk: LSBlock);
  86.     {reset the gate internal values}
  87.         var
  88.             GDH: GateDataH;
  89.     begin
  90.         GDH := GateDataH(blk.gateData);
  91.         GDH^^.oldClock := LX;
  92.         GDH^^.value := L0;
  93.         LSSetOutput(blk.env, blk.gateRef, 3, L0); {OUT}
  94.         LSSetOutput(blk.env, blk.gateRef, 4, L0); {C}
  95.     end;
  96.  
  97.     procedure DoSimulation (var blk: LSBlock);
  98.     {set the values for all outputs}
  99.         var
  100.             T, CK, OUT, C: SValue;
  101.             GDH: GateDataH;
  102.     begin
  103.         GDH := GateDataH(blk.gateData);
  104.         LSGetInput(blk.env, blk.gateRef, 1, T);
  105.         LSGetInput(blk.env, blk.gateRef, 2, CK);
  106.         if PositiveEdge(GDH^^.oldClock,ck) & (T = L1) then
  107.             GDH^^.value := LogicNOT(GDH^^.value);
  108.         if (GDH^^.value = L1) & (T = L1) then C := L1 else C:= L0;
  109.  
  110.         LSSetOutput(blk.env, blk.gateRef, 3, GDH^^.value); {OUT}
  111.         LSSetOutput(blk.env, blk.gateRef, 4, C);            {C}
  112.                  
  113.         GDH^^.oldClock := CK;
  114.     end;
  115.  
  116.  
  117.     procedure ComponentProc (var blk: LSBlock);
  118.         var
  119.             oldA4: longint;
  120.     begin
  121.  
  122.         oldA4 := SetCurrentA4;
  123.         
  124.         {we need an A4 world for storing constant strings}
  125.         {even if we have no real global variable} 
  126.         
  127.         case blk.msg of
  128.  
  129.             { class messages}
  130.             
  131.             msgNewClass: 
  132.                 DoNewClass(blk);
  133.  
  134.             msgDisposeClass: 
  135.                 DisposeHandle(Handle(ClassDataH(blk.classData)^^.pic));
  136.  
  137.             {symbol messages}
  138.             
  139.             msgDrawSymbol: 
  140.                 DoDraw(blk);
  141.  
  142.             {gate messages}
  143.             
  144.             msgNewGate:
  145.                 DoNewGate(blk);
  146.                 
  147.             msgReset:
  148.                 DoResetGate(blk);
  149.                 
  150.             msgSimulation: 
  151.                 DoSimulation(blk);
  152.  
  153.         end;
  154.  
  155.         oldA4 := SetA4(oldA4);
  156.     end;
  157.  
  158.  
  159. end.